<?php
// 广告管理
namespace Admin\Controller;
use Think\Controller;
class AdvertisementController extends Controller
{

	public function __construct($value='')
	{
		$this->isAjaxLogin();
	}

	// 广告添加/修改
	public function adChange($value='')
	{
	
		$id=I('post.id/d','');
		$data=I('post.');
		$session_id=session_id();
		$cooperation_type=I('post.cooperation_type/s','');

		$Advertisement=D('advertisement');

		$Cps=M('cps_rule');
		
		$data=$Advertisement->field('id',true)->create($data);//广告主
	 	 
	 	if (!$data) {
	 		$result['msg']=$Advertisement->getError(); 
	 		$result['code']=500;
	 		$this->ajaxReturn($result); 
	 	} 
	 	
	 	if (empty($id)) {

	 	 	
	 		$data['add_time']=time();
			$res=($data&&$Advertisement->add($data)); 
			$insert_id=$Advertisement->getLastInsID();

			if ($res) { 

				if ($cooperation_type=='cps') {

	 				M()->startTrans(); 
	 				
	 				$Advertisement->where(['session_id'=>$session_id])->save(['access_unit_price'=>0]);
	 				// $count=$Cps->where(['session_id'=>$session_id])->count();		

		 			if (C('IS_FALSE_DEL')) {
						$where['is_delete']='0';	 
					}else{
						$where=[];
					}

					$count=$Cps->where(['session_id'=>$session_id])->where($where)->count();
					if ($count==0) {
						$result['msg']='cps不能为空';		 
						$result['code']=503;
						$this->ajaxReturn($result);
					}

	 			 	$res1=$count>0?$Cps->where(['session_id'=>$session_id])->save(['ad_id'=>$insert_id]):false;
	 			 	
	 			 	$res2=$res1?$Cps->where(['ad_id'=>$insert_id])->save(['session_id'=>'']):false;
	 			 	if ($count>0&&!$res2) {
	 			 		M()->rollback();
	 			 		 
	 			 		$result['msg']='添加失败';		 
						$result['code']=501;
 		
	 			 	}else{
	 			 		M()->commit();
	 			 		$result['msg']='添加成功';		 
						$result['code']=200; 	 
	 			 	}
	 			 		 
	 			 	 
	 			}

	 			if ($cooperation_type=='cpa')
	 			 {
	 				 
	 			 	$Cps->where(['session_id'=>$session_id])->delete();
						
					 
	 			 	 
					if (empty($data['access_unit_price']))
					{
						$result['msg']='接入单价不能为空';		 
						$result['code']=504; 
						$this->ajaxReturn($result);
					}

 			 		$result['msg']='添加成功';		 
					$result['code']=200;  
					$this->ajaxReturn($result);
	 			}

					 
			}else{
				if (C('IS_FALSE_DEL')) {
					$Cps->where(['session_id'=>$session_id])->save(['is_delete'=>'1']);	 
				}else{
					$Cps->where(['session_id'=>$session_id])->delete();
				}

				$result['msg']='添加失败';		 
				$result['code']=501;
				$this->ajaxReturn($result);
			 	
			} 
			
		}else{
			
			$data=D('Advertisement')->adData($id,$data);
			// 修改广告时cps不能为空
			if ($cooperation_type=='cps') {
				 if (C('IS_FALSE_DEL')) {
					$where['is_delete']='0';	 
				}else{
					$where=[];
				}

				$count1=$Cps->where(['ad_id'=>$id])->where($where)->count();
				if ($count1==0) {
					$result['msg']='cps不能为空';		 
					$result['code']=503;
					$this->ajaxReturn($result);
				}
				 
			}

			$res=($data&&$Advertisement->where(['id'=>$id])->save($data)); 
			 
			if ($cooperation_type=='cpa') {
				 
				if (C('IS_FALSE_DEL')) {
					$Cps->where(['ad_id'=>$id])->save(['is_delete'=>'1']);	 
				}else{
					 
					$res1=$count>0?$Cps->where(['ad_id'=>$id])->delete():false;
				}
				
			}

			 
		 
			if ($res||session('?ad_'.$id)){
				$result['msg']='修改成功';		 
				$result['code']=200;
				session('ad_'.$id,null);
		 	 	
			}else{
			 	$result['msg']='修改失败';		 
				$result['code']=502;
			}
		}
	 		
			

	    $this->ajaxReturn($result);

	}

	// 广告主下拉列表
	public function selectList($value='')
	{
		$where=[];

		if (C('IS_FALSE_DEL')) {
        	$where['is_delete']='0';	 
        }else{
        	$where=[];
        }

		$data=M('advertiser')->field('id,company_name')->where($where)->select();

		$result['data']=$data;

		// if (!empty($data)) {
			$result['msg']='查询成功';
			$result['code']=200;
			 
		// }else{

		// 	$result['msg']='暂无数据';
		// 	$result['code']=501;
		// }
		$this->ajaxReturn($result);	 

	}

	
	// 广告列表
	public function adList($value='')
	{
	 
	
		$page=I('get.page',1);
		$page_size=I('get.page_size');
		$adv_id=I('get.adv_id/d');//广告主id
		$shelf_status=I('get.shelf_status/s');
		 
		$ad_name=I('get.ad_name/s','');

		if (C('IS_FALSE_DEL')) {
			$where['a.is_delete']='0';
			$where['b.is_delete']='0';
		}else{
			$where=[];
		}

		if (!empty($adv_id)&&($adv_id>-1)) {
			$where['b.id']=$adv_id;
		}
 
		if (in_array($shelf_status,['0','1'])) {
			$where['a.shelf_status']=$shelf_status;	 
		}

		if (!empty($ad_name)) {
			$where['a.ad_name'] = array('like', '%' . $ad_name . '%');	 
		}

		 
		 
		$model=M('advertisement')
		->alias('a')
		->join('wm_advertiser b on a.advertiser_id=b.id')->field('b.company_name,b.contact_name,b.contact_phone,b.account_status,b.balance,a.settlement_type,a.shelf_status,a.id,cooperation_type,a.ad_name')->where($where);
		$res=my_page($model,$page,$page_size);

		$result['data']['total_page']=$res['count'];
		$result['data']['list']=$res['data'];

		// if (!empty($res['data'])) {
			$result['msg']='查询成功';
			$result['code']=200;
		// }else{
		// 	$result['msg']='暂无数据';
		// 	$result['code']=500;
		// }


		$this->ajaxReturn($result);
	}

	// 广告删除
	public function AdDelById($value='')
	{
		$id=I('get.ad_id');

		$shelf_status=M('advertisement')->where(['id'=>$id])->getField('shelf_status');
		if ($shelf_status=='1'){
			$result['msg']='下架后才能删除';
	 		$result['code']=501;
	 		$this->ajaxReturn($result);	
		}
		
		if (C('IS_FALSE_DEL')) 
		{
			$where['is_delete']='0';	 
		}
		else
		{
			$where=[];
		}

		$where['ad_id']=$id;
		$count=M('packet')->where($where)->count();

		if ($count>0)
		{
			$result['msg']='有渠道包绑定不能删除';
			$result['code']=502;
			$this->ajaxReturn($result);
		}

		M()->startTrans(); 
		$cooperation_type=M('advertisement')->where()->getField('cooperation_type');

		$res1=my_del('advertisement',['id'=>$id]); 
		if ($cooperation_type=='cps') {
			$res2=my_del('cps_rule',['ad_id'=>$id]);  
			if ($res1&&$res2) {
		 		M()->commit();	 
		 		$result['msg']='删除成功';
		 		$result['code']=200;
			}else{
				M()->rollback();
		 		$result['msg']='删除失败';
		 		$result['code']=500;
			}
			
		}

		if ($res1) {
	 		M()->commit();	 
	 		$result['msg']='删除成功';
	 		$result['code']=200;
		}else{
			M()->rollback();
	 		$result['msg']='删除失败';
	 		$result['code']=500;
		}






	 			 		 
	 			  
		$this->ajaxReturn($result);
	}


	// 广告详情
	public function adInfo($value='')
	{
		$id=I('get.id');
		$res=$model=M('advertisement')->find($id);
		
		// if (!empty($res)) {
			$result['msg']='查询成功';
			$result['code']=200;
			$result['data']=$res;
		// }else{
		// 	$result['msg']='查询失败';
		// 	$result['code']=500;

		// }

		$this->ajaxReturn($result);
	}
	 
	 // 广告上架/下架   
	public function shelfStatus($value='')
	{
		$id=I('get.id/d');
		$shelf_status=I('get.shelf_status/s');
		// 防止重复操作
		$res2=D('Advertisement')->checkShelfStatus($id,$shelf_status);
		if (!$res2) {
			$result['msg']='不能重复操作';
			$result['code']=500;
			$this->ajaxReturn($result);
		}
		// 验证上架条件
		
		$res1=D('Advertisement')->checkAdOnShelf($id,$shelf_status);

		if (!$res1) {
			 $result['msg']='下架后至少差一天才能上架,并且上架时间必须大于下架时间,但不能大于今天';
			 $result['code']=500;
			 $this->ajaxReturn($result);
		}

		$model=M('advertisement'); 

		if (C('IS_FALSE_DEL')) 
		{
			$where['is_delete']='0';	 
		}
		else
		{
			$where=[];
		}


		$count=M('channel_packet')->where(['ad_id'=>$id,'bound_state'=>'1'])->where($where)->count();
		
		 
		if ($count>0&&$shelf_status==0)
		{
			$result['code']=503;	
			$result['msg']='渠道包正在使用该广告，不能下架';
			$this->ajaxReturn($result);	
		}

		$res=$model->where(['id'=>$id])->save(['shelf_status'=>$shelf_status]);
		 
	 
		if ($res) {
			
			if ($shelf_status==1) {	 
				$model->where(['id'=>$id])->save(['true_shelf_time'=>time()]);
				// 上架成功生成广告主数据
				D('Advertisement')->createOnData($id);
			}


			if ($shelf_status==0) {	 
				$model->where(['id'=>$id])->save(['off_shelf_time'=>time()]);
				// 生成下架数据
				D('Advertisement')->createOffData($id);
			}
			
			$result['code']=200;
			$result['msg']=($shelf_status==1) ? '上架成功':'下架成功';
		}else{
			$result['code']=($shelf_status==1) ? 501 : 502;
			$result['msg']=($shelf_status==1) ? '上架失败' : '下架失败';
		}
		 

		$this->ajaxReturn($result);
 
	}

	// 广告下拉列表
	public function adNameList($value='')
	{

		if (C('IS_FALSE_DEL')) 
		{
			$where['is_delete']='0';	 
		}
		else
		{
			$where=[];
		}

		$data=M('advertisement')->field('id,ad_name')->where($where)->select();
		// if (!empty($data)) {
			$result['msg']='查询成功';
			$result['code']=200;
			$result['data']=$data;
		// }else{
		// 	$result['msg']='查询失败';
		// 	$result['code']=500;
		// }

		$this->ajaxReturn($result);
	}

	 
	 


}